<?php


namespace app\system\service;

class AppService
{
	public static function getlist($options)
	{
		$where = [];
		if (isset($options["id"]) && $options["id"] != "") {
			$where[] = ["id", "=", $options["id"]];
		}
		if (isset($options["type"]) && $options["type"] != "" && $options["type"] != "*") {
			$where[] = ["type", "=", $options["type"]];
		}
		if (isset($options["cid"]) && $options["cid"] != "") {
			$where[] = ["cid", "=", $options["cid"]];
		}
		if (isset($options["name"]) && $options["name"] != "") {
			$where[] = ["name", "like", "%" . $options["name"] . "%"];
		}
		$list = \think\Db::name("app")->where($where)->order(["sort" => "desc", "id" => "asc"])->page($options["page"], $options["limit"])->select();
		$count = \think\Db::name("app")->where($where)->count();
		$pages = ceil($count / $options["limit"]);
		$data = [];
		foreach ($list as $item) {
			$data[] = self::get_array_info($item);
		}
		return ["code" => \app\system\model\Code::success, "data" => $data, "count" => $count, "pages" => $pages];
	}
	public static function get_array_info($item)
	{
		$minimum_active_time = getUserMinimumActiveTime($item);
		if ($item["type"] === 0) {
			$userTotal = \think\Db::name("app_card_user")->where(["appid" => $item["id"]])->count();
			$onlineTotal = \think\Db::name("app_card_user")->where([["appid", "=", $item["id"]], ["acttime", ">", $minimum_active_time], ["force_offline_status", "=", 0]])->count();
		} elseif ($item["type"] === 1) {
			$userTotal = \think\Db::name("app_member_user")->where(["appid" => $item["id"]])->count();
			$onlineTotal = \think\Db::name("app_member_user")->where([["appid", "=", $item["id"]], ["acttime", ">", $minimum_active_time], ["force_offline_status", "=", 0]])->count();
		} elseif ($item["type"] === 2) {
			$userTotal = \think\Db::name("app_auth_user")->where(["appid" => $item["id"]])->count();
			$onlineTotal = \think\Db::name("app_auth_user")->where([["appid", "=", $item["id"]], ["acttime", ">", $minimum_active_time], ["force_offline_status", "=", 0]])->count();
		}
		$data = ["id" => $item["id"], "name" => $item["name"], "avatar" => $item["avatar"], "cid" => $item["cid"], "type" => $item["type"], "addtime" => $item["addtime"], "appkey" => $item["appkey"], "is_default" => $item["is_default"], "user_verify_mode" => $item["user_verify_mode"], "param_extend_config" => (object) (json_decode($item["param_extend_config"], true) ?? []), "statistics" => ["onlineTotal" => $onlineTotal, "agentAuthorizeTotal" => \think\Db::name("app_agent_binding")->where("appid", $item["id"])->count(), "userTotal" => $userTotal], "classify_info" => \think\Db::name("app_classify")->where(["id" => $item["cid"]])->find() ?? (object) [], "sort" => $item["sort"]];
		return $data;
	}
	public static function getinfo($options)
	{
		$item = \think\Db::name("app")->where(["id" => $options["id"]])->find();
		if (empty($item)) {
			return ["code" => \app\system\model\Code::fail, "msg" => "id不存在"];
		}
		return ["code" => \app\system\model\Code::success, "data" => self::get_array_info($item)];
	}
	public static function add($options)
	{
		\think\Db::startTrans();
		try {
			$isSystemAuthUnpaid = isSystemAuthUnpaid();
			if ($isSystemAuthUnpaid && \think\Db::name("app")->count() >= \app\common\model\FreeQuota::AppQuota) {
				return ["code" => \app\system\model\Code::fail, "msg" => "当前授权为基础版,最多只能拥有" . \app\common\model\FreeQuota::AppQuota . "个应用" . \app\common\model\DefaultConfig::SystemAuthUnpaidSuffixText];
			}
			if (!empty($options["cid"]) && !\think\Db::name("app_classify")->where(["id" => $options["cid"]])->find()) {
				return ["code" => \app\system\model\Code::error, "msg" => "该分类不存在"];
			}
			$appid = \think\Db::name("app")->insertGetId(["name" => $options["name"], "type" => $options["type"], "avatar" => $options["avatar"], "user_verify_mode" => $options["user_verify_mode"] ?? 0, "addtime" => time(), "appkey" => md5(time()), "cid" => $options["cid"] ?? 0, "sort" => $options["sort"] ?? 0]);
			if ($options["type"] === 1) {
				\think\Db::name("app_member_user_level")->insert(["appid" => $appid, "name" => "默认等级", "level" => 1, "integral" => 1, "addtime" => 0, "remarks" => "此等级为系统自动创建", "upgrade" => 1, "is_default" => 1]);
			}
			if (!empty($options["give_agent_binding_type"])) {
				$give_agent_binding_type_sql_where[] = ["disable", "=", 0];
				if ($options["give_agent_binding_type"] === 2) {
					if (!isset($options["give_agent_binding_condition_type"])) {
						throw new \Exception("请选择赋予代理授权应用绑定的代理条件");
					}
					if (empty($options["give_agent_binding_condition_value"])) {
						throw new \Exception("请输入赋予代理授权应用绑定的代理条件等级");
					}
					if ($options["give_agent_binding_condition_type"] === 0) {
						if ($options["give_agent_binding_condition_value"] !== 1) {
							throw new \Exception("由于代理层级关系复杂原因本条件指定层级只能输入1级");
						}
						$give_agent_binding_type_sql_where[] = ["level", "=", $options["give_agent_binding_condition_value"]];
					} elseif ($options["give_agent_binding_condition_type"] === 1) {
						throw new \Exception("由于代理层级关系复杂原因本条件暂时不可使用");
						$give_agent_binding_type_sql_where[] = ["level", ">=", $options["give_agent_binding_condition_value"]];
					} elseif ($options["give_agent_binding_condition_type"] === 2) {
						$give_agent_binding_type_sql_where[] = ["level", "<=", $options["give_agent_binding_condition_value"]];
					}
				}
				$give_agent_binding_agent_list = \think\Db::name("app_agent")->where($give_agent_binding_type_sql_where)->select();
				foreach ($give_agent_binding_agent_list as $give_agent_binding_agent_item) {
					\think\Db::name("app_agent_binding")->insert(["appid" => $appid, "agent_id" => $give_agent_binding_agent_item["id"], "superior_id" => $give_agent_binding_agent_item["superior_id"]]);
				}
			}
			\think\Db::commit();
			return ["code" => \app\system\model\Code::success];
		} catch (\Exception $e) {
			\think\Db::rollback();
			return ["code" => \app\system\model\Code::error, "msg" => $e->getMessage()];
		}
	}
	public static function edit($options)
	{
		\think\Db::startTrans();
		try {
			$app = \think\Db::name("app")->where(["id" => $options["id"]])->find();
			if (empty($app)) {
				return ["code" => \app\system\model\Code::fail, "msg" => "id不存在"];
			}
			if ($options["is_default"] == 1) {
				\think\Db::name("app")->where(["is_default" => 1])->update(["is_default" => 0]);
			}
			if (!empty($options["cid"]) && !\think\Db::name("app_classify")->where(["id" => $options["cid"]])->find()) {
				return ["code" => \app\system\model\Code::error, "msg" => "该分类不存在"];
			}
			$data = ["name" => $options["name"], "avatar" => $options["avatar"], "is_default" => $options["is_default"], "user_verify_mode" => $options["user_verify_mode"], "param_extend_config" => json_encode($options["param_extend_config"] ?? [], JSON_UNESCAPED_UNICODE), "cid" => $options["cid"] ?: $app["cid"], "sort" => $options["sort"] ?: $app["sort"]];
			if (!empty($options["reset_appkey"])) {
				$data["appkey"] = md5(time());
			}
			\think\facade\Cache::clear();
			\think\Db::name("app")->where(["id" => $options["id"]])->update($data);
			\think\Db::commit();
			return ["code" => \app\system\model\Code::success];
		} catch (\Exception $e) {
			\think\Db::rollback();
			return ["code" => \app\system\model\Code::error, "msg" => $e->getMessage()];
		}
	}
	public static function del($options)
	{
		\think\Db::startTrans();
		try {
			$new = \think\Db::name("app")->where(["id" => $options["id"]])->find();
			if (empty($new)) {
				return ["code" => \app\system\model\Code::fail, "msg" => "id不存在"];
			}
			\think\Db::name("app")->where(["id" => $options["id"]])->delete();
			$appAssociationDataTables = ["app_agent_binding", "app_api_config", "app_auth_batch", "app_auth_card", "app_auth_user", "app_auth_user_change_log", "app_auth_user_finance_log", "app_auth_user_login_machine", "app_auth_user_unbinding_log", "app_blacklist", "app_card_batch", "app_card_user", "app_card_user_finance_log", "app_card_user_login_machine", "app_card_user_unbinding_log", "app_custom_statistics_list", "app_data", "app_feedback_classify", "app_feedback_list", "app_home_ip_log", "app_member_card", "app_member_card_batch", "app_member_dynamic", "app_member_dynamic_browse_log", "app_member_dynamic_classify", "app_member_dynamic_collect_log", "app_member_dynamic_comment", "app_member_dynamic_comment_like_log", "app_member_dynamic_label", "app_member_dynamic_label_ids", "app_member_dynamic_like_log", "app_member_user", "app_member_user_finance_log", "app_member_user_level", "app_member_user_login_machine", "app_member_user_relation", "app_member_user_settlement", "app_member_user_signing_log", "app_member_user_signing_reward_log", "app_member_user_unbinding_log", "app_order", "app_variable", "app_version"];
			foreach ($appAssociationDataTables as $table) {
				\think\Db::name($table)->where(["appid" => $options["id"]])->delete();
			}
			\think\Db::commit();
			return ["code" => \app\system\model\Code::success];
		} catch (\Exception $e) {
			\think\Db::rollback();
			return ["code" => \app\system\model\Code::error, "msg" => $e->getMessage()];
		}
	}
	public static function get_encryption_config($options)
	{
		$item = \think\Db::name("app")->where(["id" => $options["id"]])->find();
		if (empty($item)) {
			return ["code" => \app\system\model\Code::fail, "msg" => "id不存在"];
		}
		$config = json_decode($item["encryption_config"], true);
		$data = ["param_name_transfer_encryption" => $config["param_name_transfer_encryption"] ?? false, "param_value_transfer_encryption" => $config["param_value_transfer_encryption"] ?? false, "api_transfer_signature_time_limit" => $config["api_transfer_signature_time_limit"] ?? false, "api_transfer_signature_time_limit_maximum" => $config["api_transfer_signature_time_limit_maximum"] ?? 300, "api_transfer_signature_time_limit_minimum" => $config["api_transfer_signature_time_limit_minimum"] ?? 300, "api_response_encryption" => $config["api_response_encryption"] ?? false, "api_data_transfer_verify_captcha" => $config["api_data_transfer_verify_captcha"] ?? false, "api_transfer_signature_type" => $config["api_transfer_signature_type"] ?? 0, "api_response_signature_type" => $config["api_response_signature_type"] ?? 0, "api_response_data_format_type" => $config["api_response_data_format_type"] ?? 0, "api_response_signature_param_string_calculation_rule" => $config["api_response_signature_param_string_calculation_rule"] ?? 0, "api_data_transfer_encryption_type" => $config["api_data_transfer_encryption_type"] ?? 0, "api_data_transfer_encryption_encoded_type" => $config["api_data_transfer_encryption_encoded_type"] ?? 0, "json_unescaped_unicode" => $config["json_unescaped_unicode"] ?? true, "api_request_verify_blacklist" => $config["api_request_verify_blacklist"] ?? false, "api_request_forwarding_open" => $config["api_request_forwarding_open"] ?? false, "api_request_forwarding_url" => $config["api_request_forwarding_url"] ?? "", "api_request_forwarding_response_data_takeover_open" => $config["api_request_forwarding_response_data_takeover_open"] ?? false, "config" => ["aes" => ["key" => $config["config"]["aes"]["key"] ?? "", "iv" => $config["config"]["aes"]["iv"] ?? "", "mode" => $config["config"]["aes"]["mode"] ?? 0, "pkcs" => $config["config"]["aes"]["pkcs"] ?? 0], "rc4" => $config["config"]["rc4"] ?? ["key" => ""], "base64" => $config["config"]["base64"] ?? ["key" => ""], "des" => ["key" => $config["config"]["des"]["key"] ?? "", "mode" => $config["config"]["des"]["mode"] ?? 0, "pkcs" => $config["config"]["des"]["pkcs"] ?? 0], "rsa" => ["pkcs" => $config["config"]["rsa"]["pkcs"] ?? 0, "service_public_key" => $config["config"]["rsa"]["service_public_key"] ?? "", "service_private_key" => $config["config"]["rsa"]["service_private_key"] ?? "", "client_public_key" => $config["config"]["rsa"]["client_public_key"] ?? "", "client_private_key" => $config["config"]["rsa"]["client_private_key"] ?? ""], "custom" => ["encrypt_function" => $config["config"]["custom"]["encrypt_function"] ?? "", "decrypt_function" => $config["config"]["custom"]["decrypt_function"] ?? ""]]];
		return ["code" => \app\system\model\Code::success, "data" => $data];
	}
	public static function set_encryption_config($options)
	{
		\think\Db::startTrans();
		try {
			$app = \think\Db::name("app")->where(["id" => $options["appid"]])->find();
			if (empty($app)) {
				return ["code" => \app\system\model\Code::fail, "msg" => "appid不存在"];
			}
			if (isset($options["config"]["reset_rsakey"]) && $options["config"]["reset_rsakey"]) {
				$rsa1 = \app\common\utils\Rsa::getNewKey($options["config"]["config"]["rsa"]["pkcs"] ?? 0);
				$rsa2 = \app\common\utils\Rsa::getNewKey($options["config"]["config"]["rsa"]["pkcs"] ?? 0);
				$options["config"]["config"]["rsa"] = ["pkcs" => $options["config"]["config"]["rsa"]["pkcs"] ?? 0, "service_public_key" => $rsa1["public_key"] ?? "", "service_private_key" => $rsa1["private_key"] ?? "", "client_public_key" => $rsa2["public_key"] ?? "", "client_private_key" => $rsa2["private_key"] ?? ""];
				unset($options["config"]["reset_rsakey"]);
			}
			\think\Db::name("app")->where(["id" => $options["appid"]])->update(["encryption_config" => json_encode($options["config"])]);
			\think\Db::commit();
			return ["code" => \app\system\model\Code::success];
		} catch (\Exception $e) {
			\think\Db::rollback();
			return ["code" => \app\system\model\Code::error, "msg" => $e->getMessage()];
		}
	}
	public static function get_user_private_data_config($options)
	{
		$item = \think\Db::name("app")->where(["id" => $options["id"]])->find();
		if (empty($item)) {
			return ["code" => \app\system\model\Code::fail, "msg" => "id不存在"];
		}
		return ["code" => \app\system\model\Code::success, "data" => json_decode($item["user_private_data_config"], true)];
	}
	public static function set_user_private_data_config($options)
	{
		\think\Db::startTrans();
		try {
			$app = \think\Db::name("app")->where(["id" => $options["appid"]])->find();
			if (empty($app)) {
				return ["code" => \app\system\model\Code::fail, "msg" => "appid不存在"];
			}
			\think\Db::name("app")->where(["id" => $options["appid"]])->update(["user_private_data_config" => json_encode(empty($options["config"]) ? [] : $options["config"])]);
			\think\Db::commit();
			return ["code" => \app\system\model\Code::success];
		} catch (\Exception $e) {
			\think\Db::rollback();
			return ["code" => \app\system\model\Code::error, "msg" => $e->getMessage()];
		}
	}
	public static function get_user_safe_config($options)
	{
		$item = \think\Db::name("app")->where(["id" => $options["id"]])->find();
		if (empty($item)) {
			return ["code" => \app\system\model\Code::fail, "msg" => "id不存在"];
		}
		$config = json_decode($item["user_safe_config"], true);
		$data = ["token_verify_open" => $config["token_verify_open"] ?? false, "token_unlimited" => $config["token_unlimited"] ?? true, "machine_code_binding_top_limit" => $config["machine_code_binding_top_limit"] ?? \app\common\model\DefaultConfig::UserMachineCodeBindingTopLimit, "online_machine_upper_limit" => $config["online_machine_upper_limit"] ?? \app\common\model\DefaultConfig::UserOnlineMachineUpperLimit, "online_machine_upper_limit_login_scheme" => $config["online_machine_upper_limit_login_scheme"] ?? 0, "token_invalid_duration" => $config["token_invalid_duration"] ?? "", "machine_code_verify" => $config["machine_code_verify"] ?? false, "ip_verify" => $config["ip_verify"] ?? false, "ip_binding_top_limit" => $config["ip_binding_top_limit"] ?? \app\common\model\DefaultConfig::UserIpUpperLimit, "machine_ip_binding_top_limit" => $config["machine_ip_binding_top_limit"] ?? \app\common\model\DefaultConfig::UserMachineIpBindingTopLimit, "online_time_difference" => $config["online_time_difference"] ?? \app\common\model\DefaultConfig::AppUserHeartbeatInterval, "token_update_mechanism" => $config["token_update_mechanism"] ?? 0, "self_service_unbinding" => ["open" => $config["self_service_unbinding"]["open"] ?? false, "verify_user_token" => $config["self_service_unbinding"]["verify_user_token"] ?? false, "verify_ip" => $config["self_service_unbinding"]["verify_ip"] ?? false, "clear_machine_binding_ip_list" => $config["self_service_unbinding"]["clear_machine_binding_ip_list"] ?? false, "clear_binding_ip_list" => $config["self_service_unbinding"]["clear_binding_ip_list"] ?? false, "interval" => $config["self_service_unbinding"]["interval"] ?? 0, "frequency_limit_type" => $config["self_service_unbinding"]["frequency_limit_type"] ?? 0, "frequency_limit_value" => $config["self_service_unbinding"]["frequency_limit_value"] ?? 0, "deduction_time" => $config["self_service_unbinding"]["deduction_time"] ?? 0, "verify_original_machine" => $config["self_service_unbinding"]["verify_original_machine"] ?? false]];
		return ["code" => \app\system\model\Code::success, "data" => $data];
	}
	public static function set_user_safe_config($options)
	{
		\think\Db::startTrans();
		try {
			$app = \think\Db::name("app")->where(["id" => $options["appid"]])->find();
			if (empty($app)) {
				return ["code" => \app\system\model\Code::fail, "msg" => "appid不存在"];
			}
			\think\Db::name("app")->where(["id" => $options["appid"]])->update(["user_safe_config" => json_encode($options["config"])]);
			\think\Db::commit();
			return ["code" => \app\system\model\Code::success];
		} catch (\Exception $e) {
			\think\Db::rollback();
			return ["code" => \app\system\model\Code::error, "msg" => $e->getMessage()];
		}
	}
	public static function get_agent_config($options)
	{
		$item = \think\Db::name("app")->where(["id" => $options["id"]])->find();
		if (empty($item)) {
			return ["code" => \app\system\model\Code::fail, "msg" => "id不存在"];
		}
		$data = json_decode($item["agent_config"], true) ?? [];
		$data["price"] = (object) ($data["price"] ?? []);
		$data["add_card_minimum_face_value_config"] = (object) ($data["add_card_minimum_face_value_config"] ?? []);
		$data["add_card_maximum_face_value_config"] = (object) ($data["add_card_maximum_face_value_config"] ?? []);
		$data["add_card_minimum_quantity_config"] = (object) ($data["add_card_minimum_quantity_config"] ?? []);
		$data["add_card_maximum_quantity_config"] = (object) ($data["add_card_maximum_quantity_config"] ?? []);
		$data["hide_show_config"] = (object) ($data["hide_show_config"] ?? []);
		$data["add_card_structure_head_config"] = (object) ($data["add_card_structure_head_config"] ?? []);
		$data["add_card_structure_body_config"] = (object) ($data["add_card_structure_body_config"] ?? []);
		$data["add_card_structure_body_length_config"] = (object) ($data["add_card_structure_body_length_config"] ?? []);
		$data["add_card_structure_body_transform_config"] = (object) ($data["add_card_structure_body_transform_config"] ?? []);
		$data["add_card_structure_foot_config"] = (object) ($data["add_card_structure_foot_config"] ?? []);
		$data["agent_price_difference_bonus_proportion_type"] = $data["agent_price_difference_bonus_proportion_type"] ?? 0;
		$data["agent_price_difference_bonus_proportion"] = $data["agent_price_difference_bonus_proportion"] ?? 0;
		$data["agent_price_difference_bonus_hierarchy_limit_type"] = $data["agent_price_difference_bonus_hierarchy_limit_type"] ?? 0;
		$data["agent_price_difference_bonus_hierarchy_limit"] = $data["agent_price_difference_bonus_hierarchy_limit"] ?? 0;
		if ($item["type"] == 0) {
			$data["card_unbinding"] = $data["card_unbinding"] ?? false;
			$data["card_disable_enable"] = $data["card_disable_enable"] ?? false;
			$data["card_delete"] = $data["card_delete"] ?? false;
			$data["card_batch_delete"] = $data["card_batch_delete"] ?? false;
		}
		return ["code" => \app\system\model\Code::success, "data" => $data];
	}
	public static function set_agent_config($options)
	{
		\think\Db::startTrans();
		try {
			$app = \think\Db::name("app")->where(["id" => $options["appid"]])->find();
			if (empty($app)) {
				return ["code" => \app\system\model\Code::fail, "msg" => "appid不存在"];
			}
			\think\Db::name("app")->where(["id" => $options["appid"]])->update(["agent_config" => json_encode($options["config"])]);
			\think\Db::commit();
			return ["code" => \app\system\model\Code::success];
		} catch (\Exception $e) {
			\think\Db::rollback();
			return ["code" => \app\system\model\Code::error, "msg" => $e->getMessage()];
		}
	}
	public static function get_app_data($options)
	{
		$app = \think\Db::name("app")->where(["id" => $options["appid"]])->find();
		if (empty($app)) {
			return ["code" => \app\system\model\Code::fail, "msg" => "appid不存在"];
		}
		$today = \think\helper\Time::today();
		$yesterday = \think\helper\Time::yesterday();
		$week = \think\helper\Time::week();
		$lastWeek = \think\helper\Time::lastWeek();
		$month = \think\helper\Time::month();
		$lastMonth = \think\helper\Time::lastMonth();
		$year = \think\helper\Time::year();
		$lastYear = \think\helper\Time::lastYear();
		$today_Ymd = date("Ymd");
		$yesterday_Ymd = date("Ymd", strtotime("-1 day"));
		if ($app["type"] === 0) {
			$hour24 = ["dates" => DataService::getHourDate(false), "names" => DataService::getHourDate(true), "list" => ["CardAppUserActive" => DataService::getHourData($options["appid"], \time(), \app\common\model\AppData::CardAppUserActive), "CardAppUserActivation" => DataService::getHourData($options["appid"], \time(), \app\common\model\AppData::CardAppUserActivation)]];
			$day7 = ["dates" => DataService::getDayDate(time(), 7, false), "names" => DataService::getDayDate(time(), 7, true), "list" => ["CardAppUserActive" => DataService::getDayData($options["appid"], \time(), 7, \app\common\model\AppData::CardAppUserActive), "CardAppUserActivation" => DataService::getDayData($options["appid"], \time(), 7, \app\common\model\AppData::CardAppUserActivation)]];
			$statistics = ["CardAppUserActive" => ["today" => \app\common\server\StatisticsData::get($options["appid"], \app\common\model\AppData::CardAppUserActive, "Ymd", $today_Ymd, 0), "yesterday" => \app\common\server\StatisticsData::get($options["appid"], \app\common\model\AppData::CardAppUserActive, "Ymd", $yesterday_Ymd, 0)], "CardAppUserActivation" => ["today" => \app\common\server\StatisticsData::get($options["appid"], \app\common\model\AppData::CardAppUserActivation, "Ymd", $today_Ymd, 0), "yesterday" => \app\common\server\StatisticsData::get($options["appid"], \app\common\model\AppData::CardAppUserActivation, "Ymd", $yesterday_Ymd, 0)]];
		} elseif ($app["type"] === 1) {
			$hour24 = ["dates" => DataService::getHourDate(false), "names" => DataService::getHourDate(true), "list" => ["MemberAppUserActive" => DataService::getHourData($options["appid"], \time(), \app\common\model\AppData::MemberAppUserActive), "MemberAppUserRegister" => DataService::getHourData($options["appid"], \time(), \app\common\model\AppData::MemberAppUserRegister)]];
			$day7 = ["dates" => DataService::getDayDate(time(), 7, false), "names" => DataService::getDayDate(time(), 7, true), "list" => ["MemberAppUserActive" => DataService::getDayData($options["appid"], \time(), 7, \app\common\model\AppData::MemberAppUserActive), "MemberAppUserRegister" => DataService::getDayData($options["appid"], \time(), 7, \app\common\model\AppData::MemberAppUserRegister)]];
			$statistics = ["MemberAppUserActive" => ["today" => \app\common\server\StatisticsData::get($options["appid"], \app\common\model\AppData::MemberAppUserActive, "Ymd", $today_Ymd, 0), "yesterday" => \app\common\server\StatisticsData::get($options["appid"], \app\common\model\AppData::MemberAppUserActive, "Ymd", $yesterday_Ymd, 0)], "MemberAppUserRegister" => ["today" => \app\common\server\StatisticsData::get($options["appid"], \app\common\model\AppData::MemberAppUserRegister, "Ymd", $today_Ymd, 0), "yesterday" => \app\common\server\StatisticsData::get($options["appid"], \app\common\model\AppData::MemberAppUserRegister, "Ymd", $yesterday_Ymd, 0)]];
		} elseif ($app["type"] === 2) {
			$hour24 = ["dates" => DataService::getHourDate(false), "names" => DataService::getHourDate(true), "list" => ["AuthAppUserActive" => DataService::getHourData($options["appid"], \time(), \app\common\model\AppData::AuthAppUserActive), "AuthAppUserActivation" => DataService::getHourData($options["appid"], \time(), \app\common\model\AppData::AuthAppNewUserActivation)]];
			$day7 = ["dates" => DataService::getDayDate(time(), 7, false), "names" => DataService::getDayDate(time(), 7, true), "list" => ["AuthAppUserActive" => DataService::getDayData($options["appid"], \time(), 7, \app\common\model\AppData::AuthAppUserActive), "AuthAppUserActivation" => DataService::getDayData($options["appid"], \time(), 7, \app\common\model\AppData::AuthAppNewUserActivation)]];
			$statistics = ["AuthAppUserActive" => ["today" => \app\common\server\StatisticsData::get($options["appid"], \app\common\model\AppData::AuthAppUserActive, "Ymd", $today_Ymd, 0), "yesterday" => \app\common\server\StatisticsData::get($options["appid"], \app\common\model\AppData::AuthAppUserActive, "Ymd", $yesterday_Ymd, 0)], "AuthAppUserActivation" => ["today" => \app\common\server\StatisticsData::get($options["appid"], \app\common\model\AppData::AuthAppNewUserActivation, "Ymd", $today_Ymd, 0), "yesterday" => \app\common\server\StatisticsData::get($options["appid"], \app\common\model\AppData::AuthAppNewUserActivation, "Ymd", $yesterday_Ymd, 0)]];
		}
		$data["chart"] = ["hour" => $hour24 ?? [], "day" => $day7 ?? []];
		$cardDataTable = "app_card_user";
		$cardDataTableWhere = [["appid", "=", $options["appid"]]];
		if ($app["type"] == 1) {
			$cardDataTable = "app_member_card";
			$cardDataTableWhere = [["appid", "=", $options["appid"]], ["type", "=", \app\common\model\CardType::Time[0]]];
		} elseif ($app["type"] == 2) {
			$cardDataTable = "app_auth_card";
			$cardDataTableWhere = [["appid", "=", $options["appid"]], ["type", "=", \app\common\model\CardType::Time[0]]];
		}
		$statistics["agentData"] = ["authBindingAgent" => ["total" => \think\Db::name("app_agent_binding")->where(["appid" => $options["appid"]])->count(), "today" => \think\Db::name("app_agent_binding")->where([["appid", "=", $options["appid"]], ["time", "between", $today]])->count(), "yesterday" => \think\Db::name("app_agent_binding")->where([["appid", "=", $options["appid"]], ["time", "between", $yesterday]])->count()], "addCardData" => ["total" => \think\Db::name($cardDataTable)->where([["appid", "=", $options["appid"]], ["agent_id", "<>", 0]])->count(), "today" => \think\Db::name($cardDataTable)->where([["appid", "=", $options["appid"]], ["addtime", "between", $today], ["agent_id", "<>", 0]])->count(), "yesterday" => \think\Db::name($cardDataTable)->where([["appid", "=", $options["appid"]], ["addtime", "between", $yesterday], ["agent_id", "<>", 0]])->count()]];
		$statistics["cardData"] = ["total" => \think\Db::name($cardDataTable)->where(["appid" => $options["appid"]])->count(), "today" => \think\Db::name($cardDataTable)->where([["appid", "=", $options["appid"]], ["addtime", "between", $today]])->count(), "yesterday" => \think\Db::name($cardDataTable)->where([["appid", "=", $options["appid"]], ["addtime", "between", $yesterday]])->count(), "used_total" => ["total" => \think\Db::name($cardDataTable)->where([["appid", "=", $options["appid"]], ["status", "=", 1]])->count(), "today" => \think\Db::name($cardDataTable)->where([["appid", "=", $options["appid"]], ["status", "=", 1], ["addtime", "between", $today]])->count(), "yesterday" => \think\Db::name($cardDataTable)->where([["appid", "=", $options["appid"]], ["status", "=", 1], ["addtime", "between", $yesterday]])->count()], "not_used_total" => ["total" => \think\Db::name($cardDataTable)->where([["appid", "=", $options["appid"]], ["status", "=", 0]])->count(), "today" => \think\Db::name($cardDataTable)->where([["appid", "=", $options["appid"]], ["status", "=", 0], ["addtime", "between", $today]])->count(), "yesterday" => \think\Db::name($cardDataTable)->where([["appid", "=", $options["appid"]], ["status", "=", 0], ["addtime", "between", $yesterday]])->count()], "minute_total" => ["total" => \think\Db::name($cardDataTable)->where(array_merge($cardDataTableWhere, [["time_type", "=", \app\common\model\CardTimeType::Minute[0]]]))->count(), "today" => \think\Db::name($cardDataTable)->where(array_merge($cardDataTableWhere, [["time_type", "=", \app\common\model\CardTimeType::Minute[0]], ["addtime", "between", $today]]))->count(), "yesterday" => \think\Db::name($cardDataTable)->where(array_merge($cardDataTableWhere, [["time_type", "=", \app\common\model\CardTimeType::Minute[0]], ["addtime", "between", $yesterday]]))->count()], "hour_total" => ["total" => \think\Db::name($cardDataTable)->where(array_merge($cardDataTableWhere, [["time_type", "=", \app\common\model\CardTimeType::Hour[0]]]))->count(), "today" => \think\Db::name($cardDataTable)->where(array_merge($cardDataTableWhere, [["time_type", "=", \app\common\model\CardTimeType::Hour[0]], ["addtime", "between", $today]]))->count(), "yesterday" => \think\Db::name($cardDataTable)->where(array_merge($cardDataTableWhere, [["time_type", "=", \app\common\model\CardTimeType::Hour[0]], ["addtime", "between", $yesterday]]))->count()], "day_total" => ["total" => \think\Db::name($cardDataTable)->where(array_merge($cardDataTableWhere, [["time_type", "=", \app\common\model\CardTimeType::Day[0]]]))->count(), "today" => \think\Db::name($cardDataTable)->where(array_merge($cardDataTableWhere, [["time_type", "=", \app\common\model\CardTimeType::Day[0]], ["addtime", "between", $today]]))->count(), "yesterday" => \think\Db::name($cardDataTable)->where(array_merge($cardDataTableWhere, [["time_type", "=", \app\common\model\CardTimeType::Day[0]], ["addtime", "between", $yesterday]]))->count()], "week_total" => ["total" => \think\Db::name($cardDataTable)->where(array_merge($cardDataTableWhere, [["time_type", "=", \app\common\model\CardTimeType::Week[0]]]))->count(), "today" => \think\Db::name($cardDataTable)->where(array_merge($cardDataTableWhere, [["time_type", "=", \app\common\model\CardTimeType::Week[0]], ["addtime", "between", $today]]))->count(), "yesterday" => \think\Db::name($cardDataTable)->where(array_merge($cardDataTableWhere, [["time_type", "=", \app\common\model\CardTimeType::Week[0]], ["addtime", "between", $yesterday]]))->count()], "month_total" => ["total" => \think\Db::name($cardDataTable)->where(array_merge($cardDataTableWhere, [["time_type", "=", \app\common\model\CardTimeType::Month[0]]]))->count(), "today" => \think\Db::name($cardDataTable)->where(array_merge($cardDataTableWhere, [["time_type", "=", \app\common\model\CardTimeType::Month[0]], ["addtime", "between", $today]]))->count(), "yesterday" => \think\Db::name($cardDataTable)->where(array_merge($cardDataTableWhere, [["time_type", "=", \app\common\model\CardTimeType::Month[0]], ["addtime", "between", $yesterday]]))->count()], "quarter_total" => ["total" => \think\Db::name($cardDataTable)->where(array_merge($cardDataTableWhere, [["time_type", "=", \app\common\model\CardTimeType::Quarter[0]]]))->count(), "today" => \think\Db::name($cardDataTable)->where(array_merge($cardDataTableWhere, [["time_type", "=", \app\common\model\CardTimeType::Quarter[0]], ["addtime", "between", $today]]))->count(), "yesterday" => \think\Db::name($cardDataTable)->where(array_merge($cardDataTableWhere, [["time_type", "=", \app\common\model\CardTimeType::Quarter[0]], ["addtime", "between", $yesterday]]))->count()], "half_year_total" => ["total" => \think\Db::name($cardDataTable)->where(array_merge($cardDataTableWhere, [["time_type", "=", \app\common\model\CardTimeType::HalfYear[0]]]))->count(), "today" => \think\Db::name($cardDataTable)->where(array_merge($cardDataTableWhere, [["time_type", "=", \app\common\model\CardTimeType::HalfYear[0]], ["addtime", "between", $today]]))->count(), "yesterday" => \think\Db::name($cardDataTable)->where(array_merge($cardDataTableWhere, [["time_type", "=", \app\common\model\CardTimeType::HalfYear[0]], ["addtime", "between", $yesterday]]))->count()], "year_total" => ["total" => \think\Db::name($cardDataTable)->where(array_merge($cardDataTableWhere, [["time_type", "=", \app\common\model\CardTimeType::Year[0]]]))->count(), "today" => \think\Db::name($cardDataTable)->where(array_merge($cardDataTableWhere, [["time_type", "=", \app\common\model\CardTimeType::Year[0]], ["addtime", "between", $today]]))->count(), "yesterday" => \think\Db::name($cardDataTable)->where(array_merge($cardDataTableWhere, [["time_type", "=", \app\common\model\CardTimeType::Year[0]], ["addtime", "between", $yesterday]]))->count()], "perpetual_total" => ["total" => \think\Db::name($cardDataTable)->where(array_merge($cardDataTableWhere, [["time_type", "=", \app\common\model\CardTimeType::Perpetual[0]]]))->count(), "today" => \think\Db::name($cardDataTable)->where(array_merge($cardDataTableWhere, [["time_type", "=", \app\common\model\CardTimeType::Perpetual[0]], ["addtime", "between", $today]]))->count(), "yesterday" => \think\Db::name($cardDataTable)->where(array_merge($cardDataTableWhere, [["time_type", "=", \app\common\model\CardTimeType::Perpetual[0]], ["addtime", "between", $yesterday]]))->count()]];
		if ($app["type"] != 0) {
			$statistics["cardData"]["point_total"] = ["total" => \think\Db::name($cardDataTable)->where([["appid", "=", $options["appid"]], ["type", "=", \app\common\model\CardType::Point[0]]])->count(), "today" => \think\Db::name($cardDataTable)->where([["appid", "=", $options["appid"]], ["type", "=", \app\common\model\CardType::Point[0]], ["addtime", "between", $today]])->count(), "yesterday" => \think\Db::name($cardDataTable)->where([["appid", "=", $options["appid"]], ["type", "=", \app\common\model\CardType::Point[0]], ["addtime", "between", $yesterday]])->count()];
			if ($app["type"] == 1) {
				$statistics["cardData"]["register_code_total"] = ["total" => \think\Db::name($cardDataTable)->where([["appid", "=", $options["appid"]], ["type", "=", \app\common\model\CardType::RegisterCode[0]]])->count(), "today" => \think\Db::name($cardDataTable)->where([["appid", "=", $options["appid"]], ["type", "=", \app\common\model\CardType::RegisterCode[0]], ["addtime", "between", $today]])->count(), "yesterday" => \think\Db::name($cardDataTable)->where([["appid", "=", $options["appid"]], ["type", "=", \app\common\model\CardType::RegisterCode[0]], ["addtime", "between", $yesterday]])->count()];
			}
		} else {
			$statistics["cardData"]["expired_total"] = ["total" => \think\Db::name($cardDataTable)->where(array_merge($cardDataTableWhere, [["endtime", "<", time()], ["time_type", "=", \app\common\model\CardTimeType::Minute[0]]]))->count(), "today" => \think\Db::name($cardDataTable)->where(array_merge($cardDataTableWhere, [["time_type", "=", \app\common\model\CardTimeType::Minute[0]], ["endtime", "between", [$today[0], time()]]]))->count(), "yesterday" => \think\Db::name($cardDataTable)->where(array_merge($cardDataTableWhere, [["time_type", "=", \app\common\model\CardTimeType::Minute[0]], ["endtime", "between", $yesterday]]))->count()];
		}
		$data["statistics"] = $statistics ?? [];
		return ["code" => \app\system\model\Code::success, "data" => $data];
	}
	public static function get_member_app_config($options)
	{
		$item = \think\Db::name("app")->where(["id" => $options["id"]])->find();
		if (empty($item)) {
			return ["code" => \app\system\model\Code::fail, "msg" => "id不存在"];
		}
		$config = json_decode($item["member_app_config"], true);
		$data = ["register_config" => ["close" => $config["register_config"]["close"] ?? false, "qq_connect_close" => $config["register_config"]["qq_connect_close"] ?? false, "register_code_verify" => $config["register_config"]["register_code_verify"] ?? false, "phone_close" => $config["register_config"]["phone_close"] ?? false, "register_frequency_limit_type" => $config["register_config"]["register_frequency_limit_type"] ?? 0, "register_frequency_limit_value" => $config["register_config"]["register_frequency_limit_value"] ?? 0, "give" => ["point" => $config["register_config"]["give"]["point"] ?? 0, "growth" => $config["register_config"]["give"]["growth"] ?? 0, "viptime" => ["type" => $config["register_config"]["give"]["viptime"]["type"] ?? 0, "value" => $config["register_config"]["give"]["viptime"]["value"] ?? 0]]], "point_config" => ["cash_settlement_open" => $config["point_config"]["cash_settlement_open"] ?? false, "cash_settlement_ratio" => $config["point_config"]["cash_settlement_ratio"] ?? 0, "api_increase_open" => $config["point_config"]["api_increase_open"] ?? false, "api_reduce_open" => $config["point_config"]["api_reduce_open"] ?? false, "api_increase_maximum" => $config["point_config"]["api_increase_maximum"] ?? 0, "api_increase_minimum" => $config["point_config"]["api_increase_minimum"] ?? 0, "api_reduce_maximum" => $config["point_config"]["api_reduce_maximum"] ?? 0, "api_reduce_minimum" => $config["point_config"]["api_reduce_minimum"] ?? 0, "api_increase_frequency_limit_type" => $config["point_config"]["api_increase_frequency_limit_type"] ?? 0, "api_increase_frequency_limit_value" => $config["point_config"]["api_increase_frequency_limit_value"] ?? 0, "api_increase_quantity_limit_type" => $config["point_config"]["api_increase_quantity_limit_type"] ?? 0, "api_increase_quantity_limit_value" => $config["point_config"]["api_increase_quantity_limit_value"] ?? 0], "signing_config" => ["cycle" => $config["signing_config"]["cycle"] ?? 0, "ordinary" => ["daily" => ["gift_data" => ["viptime" => ["type" => $config["signing_config"]["ordinary"]["daily"]["gift_data"]["viptime"]["type"] ?? 0, "value" => $config["signing_config"]["ordinary"]["daily"]["gift_data"]["viptime"]["value"] ?? 0], "point" => $config["signing_config"]["ordinary"]["daily"]["gift_data"]["point"] ?? 0, "growth" => $config["signing_config"]["ordinary"]["daily"]["gift_data"]["growth"] ?? 0]], "first" => ["gift_data" => ["viptime" => ["type" => $config["signing_config"]["ordinary"]["first"]["gift_data"]["viptime"]["type"] ?? 0, "value" => $config["signing_config"]["ordinary"]["first"]["gift_data"]["viptime"]["value"] ?? 0], "point" => $config["signing_config"]["ordinary"]["first"]["gift_data"]["point"] ?? 0, "growth" => $config["signing_config"]["ordinary"]["first"]["gift_data"]["growth"] ?? 0]]], "special" => (object) ($config["signing_config"]["special"] ?? []), "total" => (object) ($config["signing_config"]["total"] ?? []), "continuity" => (object) ($config["signing_config"]["continuity"] ?? [])], "qq_connect_config" => ["mobile" => ["appid" => $config["qq_connect_config"]["mobile"]["appid"] ?? ""]], "settlement_config" => ["limit_count" => $config["settlement_config"]["limit_count"] ?? 0, "service_charge" => $config["settlement_config"]["service_charge"] ?? 0, "method" => ["alipay" => ["open" => $config["settlement_config"]["method"]["alipay"]["open"] ?? false, "minimum" => $config["settlement_config"]["method"]["alipay"]["minimum"] ?? 0, "maximum" => $config["settlement_config"]["method"]["alipay"]["maximum"] ?? 0], "wechat" => ["open" => $config["settlement_config"]["method"]["wechat"]["open"] ?? false, "minimum" => $config["settlement_config"]["method"]["wechat"]["minimum"] ?? 0, "maximum" => $config["settlement_config"]["method"]["wechat"]["maximum"] ?? 0], "bankcard" => ["open" => $config["settlement_config"]["method"]["bankcard"]["open"] ?? false, "minimum" => $config["settlement_config"]["method"]["bankcard"]["minimum"] ?? 0, "maximum" => $config["settlement_config"]["method"]["bankcard"]["maximum"] ?? 0]]]];
		return ["code" => \app\system\model\Code::success, "data" => $data];
	}
	public static function set_member_app_config($options)
	{
		\think\Db::startTrans();
		try {
			$app = \think\Db::name("app")->where(["id" => $options["appid"]])->find();
			if (empty($app)) {
				return ["code" => \app\system\model\Code::fail, "msg" => "appid不存在"];
			}
			\think\Db::name("app")->where(["id" => $options["appid"]])->update(["member_app_config" => json_encode($options["config"])]);
			\think\Db::commit();
			return ["code" => \app\system\model\Code::success];
		} catch (\Exception $e) {
			\think\Db::rollback();
			return ["code" => \app\system\model\Code::error, "msg" => $e->getMessage()];
		}
	}
	public static function get_buy_config($options)
	{
		$item = \think\Db::name("app")->where(["id" => $options["id"]])->find();
		if (empty($item)) {
			return ["code" => \app\system\model\Code::fail, "msg" => "id不存在"];
		}
		$config = json_decode($item["buy_config"], true);
		$data = ["common_buy_card" => ["close_buy_card" => $config["common_buy_card"]["close_buy_card"] ?? false, "price" => (object) ($config["common_buy_card"]["price"] ?? []), "add_card_minimum_face_value_config" => (object) ($config["common_buy_card"]["add_card_minimum_face_value_config"] ?? []), "add_card_maximum_face_value_config" => (object) ($config["common_buy_card"]["add_card_maximum_face_value_config"] ?? []), "add_card_minimum_quantity_config" => (object) ($config["common_buy_card"]["add_card_minimum_quantity_config"] ?? []), "add_card_maximum_quantity_config" => (object) ($config["common_buy_card"]["add_card_maximum_quantity_config"] ?? []), "add_card_structure_head_config" => (object) ($config["common_buy_card"]["add_card_structure_head_config"] ?? []), "add_card_structure_body_config" => (object) ($config["common_buy_card"]["add_card_structure_body_config"] ?? []), "add_card_structure_body_length_config" => (object) ($config["common_buy_card"]["add_card_structure_body_length_config"] ?? []), "add_card_structure_body_transform_config" => (object) ($config["common_buy_card"]["add_card_structure_body_transform_config"] ?? []), "add_card_structure_foot_config" => (object) ($config["common_buy_card"]["add_card_structure_foot_config"] ?? []), "query_password_must" => $config["common_buy_card"]["query_password_must"] ?? false, "card_buy_deliver_notice" => $config["common_buy_card"]["card_buy_deliver_notice"] ?? false], "common_buy_renewal" => ["close_renewal" => $config["common_buy_renewal"]["close_renewal"] ?? false, "price" => (object) ($config["common_buy_renewal"]["price"] ?? []), "add_card_minimum_face_value_config" => (object) ($config["common_buy_renewal"]["add_card_minimum_face_value_config"] ?? []), "add_card_maximum_face_value_config" => (object) ($config["common_buy_renewal"]["add_card_maximum_face_value_config"] ?? []), "add_card_minimum_quantity_config" => (object) ($config["common_buy_renewal"]["add_card_minimum_quantity_config"] ?? []), "add_card_maximum_quantity_config" => (object) ($config["common_buy_renewal"]["add_card_maximum_quantity_config"] ?? []), "add_card_structure_head_config" => (object) ($config["common_buy_renewal"]["add_card_structure_head_config"] ?? []), "add_card_structure_body_config" => (object) ($config["common_buy_renewal"]["add_card_structure_body_config"] ?? []), "add_card_structure_body_length_config" => (object) ($config["common_buy_renewal"]["add_card_structure_body_length_config"] ?? []), "add_card_structure_body_transform_config" => (object) ($config["common_buy_renewal"]["add_card_structure_body_transform_config"] ?? []), "add_card_structure_foot_config" => (object) ($config["common_buy_renewal"]["add_card_structure_foot_config"] ?? [])]];
		return ["code" => \app\system\model\Code::success, "data" => $data];
	}
	public static function set_buy_config($options)
	{
		\think\Db::startTrans();
		try {
			$app = \think\Db::name("app")->where(["id" => $options["appid"]])->find();
			if (empty($app)) {
				return ["code" => \app\system\model\Code::fail, "msg" => "appid不存在"];
			}
			\think\Db::name("app")->where(["id" => $options["appid"]])->update(["buy_config" => json_encode($options["config"])]);
			\think\Db::commit();
			return ["code" => \app\system\model\Code::success];
		} catch (\Exception $e) {
			\think\Db::rollback();
			return ["code" => \app\system\model\Code::error, "msg" => $e->getMessage()];
		}
	}
	public static function get_buy_order_list($options)
	{
		$where = [];
		$where[] = ["appid", "=", $options["appid"]];
		if (isset($options["pay_status"]) && $options["pay_status"] != "") {
			$where[] = ["pay_status", "=", $options["pay_status"]];
		}
		if (isset($options["order_no"]) && $options["order_no"] != "") {
			$where[] = ["order_no", "=", $options["order_no"]];
		}
		if (!empty($options["time_scope"]) && count($options["time_scope"]) > 1) {
			$time_scope = $options["time_scope"];
			$where[] = ["create_time", ">=", strtotime($time_scope[0])];
			$where[] = ["create_time", "<", strtotime($time_scope[1])];
		}
		$list = \think\Db::name("app_order")->where($where)->order(["id" => "desc"])->page($options["page"], $options["limit"])->select();
		$count = \think\Db::name("app_order")->where($where)->count();
		$pages = ceil($count / $options["limit"]);
		$data = [];
		foreach ($list as $item) {
			$item["goods_type"] = is_numeric($item["goods_type"]) ? \intval($item["goods_type"]) : $item["goods_type"];
			$item["goods_value"] = $item["type"] == \app\common\model\BuyType::MemberAppBuyCard || $item["type"] == \app\common\model\BuyType::CardAppBuyCard || $item["type"] == \app\common\model\BuyType::CardAppUserRenewal ? \intval($item["goods_value"]) : $item["goods_value"];
			$item["delivery_data"] = json_decode($item["delivery_data"], true) ?? [];
			$data[] = $item;
		}
		return ["code" => \app\system\model\Code::success, "data" => $data, "count" => $count, "pages" => $pages];
	}
	public static function get_card_app_config($options)
	{
		$item = \think\Db::name("app")->where(["id" => $options["id"]])->find();
		if (empty($item)) {
			return ["code" => \app\system\model\Code::fail, "msg" => "id不存在"];
		}
		$config = json_decode($item["card_app_config"], true);
		$data = ["time_config" => ["api_increase_open" => $config["time_config"]["api_increase_open"] ?? false, "api_reduce_open" => $config["time_config"]["api_reduce_open"] ?? false, "api_increase_maximum" => $config["time_config"]["api_increase_maximum"] ?? 0, "api_increase_minimum" => $config["time_config"]["api_increase_minimum"] ?? 0, "api_reduce_maximum" => $config["time_config"]["api_reduce_maximum"] ?? 0, "api_reduce_minimum" => $config["time_config"]["api_reduce_minimum"] ?? 0, "api_increase_frequency_limit_type" => $config["time_config"]["api_increase_frequency_limit_type"] ?? 0, "api_increase_frequency_limit_value" => $config["time_config"]["api_increase_frequency_limit_value"] ?? 0, "api_increase_quantity_limit_type" => $config["time_config"]["api_increase_quantity_limit_type"] ?? 0, "api_increase_quantity_limit_value" => $config["time_config"]["api_increase_quantity_limit_value"] ?? 0], "point_config" => ["api_increase_open" => $config["point_config"]["api_increase_open"] ?? false, "api_reduce_open" => $config["point_config"]["api_reduce_open"] ?? false, "api_increase_maximum" => $config["point_config"]["api_increase_maximum"] ?? 0, "api_increase_minimum" => $config["point_config"]["api_increase_minimum"] ?? 0, "api_reduce_maximum" => $config["point_config"]["api_reduce_maximum"] ?? 0, "api_reduce_minimum" => $config["point_config"]["api_reduce_minimum"] ?? 0, "api_increase_frequency_limit_type" => $config["point_config"]["api_increase_frequency_limit_type"] ?? 0, "api_increase_frequency_limit_value" => $config["point_config"]["api_increase_frequency_limit_value"] ?? 0, "api_increase_quantity_limit_type" => $config["point_config"]["api_increase_quantity_limit_type"] ?? 0, "api_increase_quantity_limit_value" => $config["point_config"]["api_increase_quantity_limit_value"] ?? 0]];
		return ["code" => \app\system\model\Code::success, "data" => $data];
	}
	public static function set_card_app_config($options)
	{
		\think\Db::startTrans();
		try {
			$app = \think\Db::name("app")->where(["id" => $options["appid"]])->find();
			if (empty($app)) {
				return ["code" => \app\system\model\Code::fail, "msg" => "appid不存在"];
			}
			\think\Db::name("app")->where(["id" => $options["appid"]])->update(["card_app_config" => json_encode($options["config"])]);
			\think\Db::commit();
			return ["code" => \app\system\model\Code::success];
		} catch (\Exception $e) {
			\think\Db::rollback();
			return ["code" => \app\system\model\Code::error, "msg" => $e->getMessage()];
		}
	}
	public static function get_auth_app_config($options)
	{
		$item = \think\Db::name("app")->where(["id" => $options["id"]])->find();
		if (empty($item)) {
			return ["code" => \app\system\model\Code::fail, "msg" => "id不存在"];
		}
		$config = json_decode($item["auth_app_config"], true);
		$data = ["time_config" => ["api_increase_open" => $config["time_config"]["api_increase_open"] ?? false, "api_reduce_open" => $config["time_config"]["api_reduce_open"] ?? false, "api_increase_maximum" => $config["time_config"]["api_increase_maximum"] ?? 0, "api_increase_minimum" => $config["time_config"]["api_increase_minimum"] ?? 0, "api_reduce_maximum" => $config["time_config"]["api_reduce_maximum"] ?? 0, "api_reduce_minimum" => $config["time_config"]["api_reduce_minimum"] ?? 0, "api_increase_frequency_limit_type" => $config["time_config"]["api_increase_frequency_limit_type"] ?? 0, "api_increase_frequency_limit_value" => $config["time_config"]["api_increase_frequency_limit_value"] ?? 0, "api_increase_quantity_limit_type" => $config["time_config"]["api_increase_quantity_limit_type"] ?? 0, "api_increase_quantity_limit_value" => $config["time_config"]["api_increase_quantity_limit_value"] ?? 0], "point_config" => ["api_increase_open" => $config["point_config"]["api_increase_open"] ?? false, "api_reduce_open" => $config["point_config"]["api_reduce_open"] ?? false, "api_increase_maximum" => $config["point_config"]["api_increase_maximum"] ?? 0, "api_increase_minimum" => $config["point_config"]["api_increase_minimum"] ?? 0, "api_reduce_maximum" => $config["point_config"]["api_reduce_maximum"] ?? 0, "api_reduce_minimum" => $config["point_config"]["api_reduce_minimum"] ?? 0, "api_increase_frequency_limit_type" => $config["point_config"]["api_increase_frequency_limit_type"] ?? 0, "api_increase_frequency_limit_value" => $config["point_config"]["api_increase_frequency_limit_value"] ?? 0, "api_increase_quantity_limit_type" => $config["point_config"]["api_increase_quantity_limit_type"] ?? 0, "api_increase_quantity_limit_value" => $config["point_config"]["api_increase_quantity_limit_value"] ?? 0], "change_binding_config" => ["open" => $config["change_binding_config"]["open"] ?? false, "attach_unbinding_machine" => $config["change_binding_config"]["attach_unbinding_machine"] ?? false, "verify_user_token" => $config["change_binding_config"]["verify_user_token"] ?? false, "verify_original_machine" => $config["change_binding_config"]["verify_original_machine"] ?? false, "interval" => $config["change_binding_config"]["interval"] ?? 0, "deduction_time" => $config["change_binding_config"]["deduction_time"] ?? 0, "frequency_limit_type" => $config["change_binding_config"]["frequency_limit_type"] ?? 0, "frequency_limit_value" => $config["change_binding_config"]["frequency_limit_value"] ?? 0]];
		return ["code" => \app\system\model\Code::success, "data" => $data];
	}
	public static function set_auth_app_config($options)
	{
		\think\Db::startTrans();
		try {
			$app = \think\Db::name("app")->where(["id" => $options["appid"]])->find();
			if (empty($app)) {
				return ["code" => \app\system\model\Code::fail, "msg" => "appid不存在"];
			}
			\think\Db::name("app")->where(["id" => $options["appid"]])->update(["auth_app_config" => json_encode($options["config"])]);
			\think\Db::commit();
			return ["code" => \app\system\model\Code::success];
		} catch (\Exception $e) {
			\think\Db::rollback();
			return ["code" => \app\system\model\Code::error, "msg" => $e->getMessage()];
		}
	}
	public static function get_classify_list($options)
	{
		$where = [];
		if (isset($options["id"]) && $options["id"] != "") {
			$where[] = ["id", "=", $options["id"]];
		}
		if (isset($options["name"]) && $options["name"] != "") {
			$where[] = ["name", "like", "%" . $options["name"] . "%"];
		}
		$list = \think\Db::name("app_classify")->where($where)->order(["sort" => "desc", "id" => "asc"])->page($options["page"], $options["limit"])->select();
		$count = \think\Db::name("app_classify")->where($where)->count();
		$pages = ceil($count / $options["limit"]);
		$data = [];
		foreach ($list as $item) {
			$data[] = self::get_classify_array_info($item);
		}
		return ["code" => \app\system\model\Code::success, "data" => $data, "count" => $count, "pages" => $pages];
	}
	public static function get_classify_array_info($item)
	{
		return $item;
	}
	public static function get_classify_info($options)
	{
		$item = \think\Db::name("app_classify")->where(["id" => $options["id"]])->find();
		if (empty($item)) {
			return ["code" => \app\system\model\Code::fail, "msg" => "id不存在"];
		}
		return ["code" => \app\system\model\Code::success, "data" => self::get_classify_array_info($item)];
	}
	public static function add_classify($options)
	{
		\think\Db::startTrans();
		try {
			\think\Db::name("app_classify")->insertGetId(["name" => $options["name"], "addtime" => time(), "sort" => $options["sort"]]);
			\think\Db::commit();
			return ["code" => \app\system\model\Code::success];
		} catch (\Exception $e) {
			\think\Db::rollback();
			return ["code" => \app\system\model\Code::error, "msg" => $e->getMessage()];
		}
	}
	public static function edit_classify($options)
	{
		\think\Db::startTrans();
		try {
			$classify = \think\Db::name("app_classify")->where(["id" => $options["id"]])->find();
			if (empty($classify)) {
				return ["code" => \app\system\model\Code::fail, "msg" => "id不存在"];
			}
			$data = ["name" => $options["name"], "sort" => $options["sort"]];
			\think\Db::name("app_classify")->where(["id" => $options["id"]])->update($data);
			\think\Db::commit();
			return ["code" => \app\system\model\Code::success];
		} catch (\Exception $e) {
			\think\Db::rollback();
			return ["code" => \app\system\model\Code::error, "msg" => $e->getMessage()];
		}
	}
	public static function del_classify($options)
	{
		\think\Db::startTrans();
		try {
			$classify = \think\Db::name("app_classify")->where(["id" => $options["id"]])->find();
			if (empty($classify)) {
				return ["code" => \app\system\model\Code::fail, "msg" => "id不存在"];
			}
			if (!!\think\Db::name("app")->where(["cid" => $options["id"]])->find()) {
				return ["code" => \app\system\model\Code::fail, "msg" => "该分类下存在应用，不支持删除"];
			}
			\think\Db::name("app_classify")->where(["id" => $options["id"]])->delete();
			\think\Db::commit();
			return ["code" => \app\system\model\Code::success];
		} catch (\Exception $e) {
			\think\Db::rollback();
			return ["code" => \app\system\model\Code::error, "msg" => $e->getMessage()];
		}
	}
	public static function get_upload_config($options)
	{
		$item = \think\Db::name("app")->where(["id" => $options["id"]])->find();
		if (empty($item)) {
			return ["code" => \app\system\model\Code::fail, "msg" => "id不存在"];
		}
		$config = json_decode($item["upload_config"], true);
		$data = ["open" => $config["open"] ?? false, "verify_graphic_code" => $config["verify_graphic_code"] ?? false, "max_size_limit" => $config["max_size_limit"] ?? 0, "max_frequency_limit" => $config["max_frequency_limit"] ?? 0, "allow_file_suffix" => $config["allow_file_suffix"] ?? []];
		return ["code" => \app\system\model\Code::success, "data" => $data];
	}
	public static function set_upload_config($options)
	{
		\think\Db::startTrans();
		try {
			$app = \think\Db::name("app")->where(["id" => $options["appid"]])->find();
			if (empty($app)) {
				return ["code" => \app\system\model\Code::fail, "msg" => "appid不存在"];
			}
			\think\Db::name("app")->where(["id" => $options["appid"]])->update(["upload_config" => json_encode($options["config"])]);
			\think\Db::commit();
			return ["code" => \app\system\model\Code::success];
		} catch (\Exception $e) {
			\think\Db::rollback();
			return ["code" => \app\system\model\Code::error, "msg" => $e->getMessage()];
		}
	}
	public static function get_home_info($options)
	{
		$app = \think\Db::name("app")->where(["id" => $options["id"]])->find();
		if (empty($app)) {
			return ["code" => \app\system\model\Code::fail, "msg" => "id不存在"];
		}
		$template = AppService::get_home_template_config_data($app);
		if ($template["code"] != \app\system\model\Code::success) {
			return $template;
		}
		$template_list = [];
		$dirs = getDirList("./app-home/");
		foreach ($dirs as $dir) {
			$configFile = "./app-home/{$dir}/config.json";
			if (is_file($configFile)) {
				$template_config = json_decode(file_get_contents($configFile), true) ?? [];
				if (isset($template_config["name"]) && isset($template_config["describe"]) && isset($template_config["config"])) {
					$template_list[] = ["name" => $template_config["name"] ?? $dir, "template" => $dir];
				}
			}
		}
		$data = ["home" => ["template" => $template["data"]["template"], "config" => $template["data"]["config"]], "domain" => $app["home_domain"], "template_list" => $template_list];
		$today_Ymd = date("Ymd");
		$yesterday_Ymd = date("Ymd", strtotime("-1 day"));
		$hour24 = ["dates" => DataService::getHourDate(false), "names" => DataService::getHourDate(true), "list" => ["uv" => DataService::getHourData($app["id"], \time(), \app\common\model\AppData::AppHomeUv), "pv" => DataService::getHourData($app["id"], \time(), \app\common\model\AppData::AppHomePv)]];
		$day7 = ["dates" => DataService::getDayDate(time(), 7, false), "names" => DataService::getDayDate(time(), 7, true), "list" => ["uv" => DataService::getDayData($app["id"], \time(), 7, \app\common\model\AppData::AppHomeUv), "pv" => DataService::getDayData($app["id"], \time(), 7, \app\common\model\AppData::AppHomePv)]];
		$statistics = ["uv" => ["today" => \app\common\server\StatisticsData::get($app["id"], \app\common\model\AppData::AppHomeUv, "Ymd", $today_Ymd, 0), "yesterday" => \app\common\server\StatisticsData::get($app["id"], \app\common\model\AppData::AppHomePv, "Ymd", $yesterday_Ymd, 0)], "pv" => ["today" => \app\common\server\StatisticsData::get($app["id"], \app\common\model\AppData::AppHomePv, "Ymd", $today_Ymd, 0), "yesterday" => \app\common\server\StatisticsData::get($app["id"], \app\common\model\AppData::AppHomePv, "Ymd", $yesterday_Ymd, 0)]];
		$data["data"] = ["statistics" => $statistics, "chart" => ["hour" => $hour24, "day" => $day7]];
		return ["code" => \app\system\model\Code::success, "data" => $data];
	}
	public static function set_home_config($options)
	{
		\think\Db::startTrans();
		try {
			$app = \think\Db::name("app")->where(["id" => $options["appid"]])->find();
			if (empty($app)) {
				return ["code" => \app\system\model\Code::fail, "msg" => "appid不存在"];
			}
			$home_config = json_decode($app["home_config"], true);
			$home_config["template"] = $options["config"]["template"];
			$home_config["config"][$options["config"]["template"]] = $options["config"]["config"];
			foreach ($home_config["config"] as $template_index => $template) {
				foreach ($template["config"] as $template_config_index => $template_config) {
					$home_config["config"][$template_index]["config"][$template_config_index] = [];
					$home_config["config"][$template_index]["config"][$template_config_index]["currentIsShow"] = $template_config["currentIsShow"];
					foreach ($template_config["config"] as $template_config_item_index => $template_config_item) {
						$home_config["config"][$template_index]["config"][$template_config_index]["config"][$template_config_item_index]["value"] = $template_config_item["value"];
					}
				}
			}
			\think\Db::name("app")->where(["id" => $options["appid"]])->update(["home_config" => json_encode($home_config, JSON_UNESCAPED_UNICODE), "home_domain" => $options["domain"] ?? ""]);
			\think\Db::commit();
			return ["code" => \app\system\model\Code::success];
		} catch (\Exception $e) {
			\think\Db::rollback();
			return ["code" => \app\system\model\Code::error, "msg" => $e->getMessage()];
		}
	}
	public static function clear_home_config($options)
	{
		\think\Db::startTrans();
		try {
			$app = \think\Db::name("app")->where(["id" => $options["appid"]])->find();
			if (empty($app)) {
				return ["code" => \app\system\model\Code::fail, "msg" => "appid不存在"];
			}
			$template = AppService::get_home_template_config_data($app, $options["template"]);
			if ($template["code"] != \app\system\model\Code::success) {
				return $template;
			}
			$home_config = json_decode($app["home_config"], true);
			if (is_array($home_config)) {
				unset($home_config["config"][$template["data"]["template"]]);
			}
			\think\Db::name("app")->where(["id" => $options["appid"]])->update(["home_config" => json_encode($home_config, JSON_UNESCAPED_UNICODE)]);
			\think\Db::commit();
			return ["code" => \app\system\model\Code::success];
		} catch (\Exception $e) {
			\think\Db::rollback();
			return ["code" => \app\system\model\Code::error, "msg" => $e->getMessage()];
		}
	}
	public static function get_home_template_config_data($app, $assignTemplate = "")
	{
		if (!is_array($app)) {
			$app = \think\Db::name("app")->where(["id" => $app])->find();
			if (empty($app)) {
				return ["code" => \app\system\model\Code::fail, "msg" => "appid不存在"];
			}
		}
		$home_config = json_decode($app["home_config"], true);
		return ["code" => \app\system\model\Code::success, "data" => get_template_config_data("./app-home/", $home_config, "config.json", $assignTemplate)];
	}
	public static function get_import_export_options($assignType = "", $assignField = "")
	{
		$appConfig = ["base" => ["name" => "基本信息"], "param_extend_config" => ["name" => "应用扩展数据"], "encryption_config" => ["name" => "接口通信与加密配置"], "user_safe_config" => ["name" => "用户安全配置"], "user_private_data_config" => ["name" => "用户私有扩展数据配置"], "feedback_config" => ["name" => "应用反馈帮助配置"], "agent_config" => ["name" => "应用代理全局配置"], "buy_config" => ["name" => "在线交易配置"], "home_config" => ["name" => "应用官网配置"], "upload_config" => ["name" => "文件上传配置"], "mail_config" => ["name" => "发信邮箱配置"], "api_code_config" => ["name" => "应用接口业务码独立配置"], "card_app_config" => ["name" => "单码应用-专项配置"], "auth_app_config" => ["name" => "授权应用-专项配置"], "member_app_config" => ["name" => "会员应用-专项配置"], "settlement_config" => ["name" => "会员应用-用户结算配置"]];
		$appTable = ["app_version" => ["name" => "应用版本数据表"], "app_variable" => ["name" => "应用变量数据表"], "app_blacklist" => ["name" => "应用黑名单数据表"], "app_api_config" => ["name" => "应用接口配置数据表"], "app_feedback_classify" => ["name" => "应用反馈分类数据表"], "app_custom_statistics_list" => ["name" => "应用自定义数据统计数据表"], "app_member_user_level" => ["name" => "会员应用-用户等级数据表"]];
		if ($assignType == "appConfig") {
			return $appConfig[$assignField];
		} elseif ($assignType == "appTable") {
			return $appTable[$assignField];
		}
		return ["appConfig" => $appConfig, "appTable" => $appTable];
	}
	public static function export_app_config_data($options)
	{
		$app = \think\Db::name("app")->where(["id" => $options["appid"]])->find();
		if (empty($app)) {
			return ["code" => \app\system\model\Code::fail, "msg" => "id不存在"];
		}
		$appConfig = [];
		if (is_array($options["appConfig"])) {
			foreach ($options["appConfig"] as $appConfigField) {
				if ($appConfigField === "base") {
					$appConfig["base"] = ["name" => $app["name"], "avatar" => $app["avatar"], "type" => $app["type"], "appkey" => $app["appkey"], "user_verify_mode" => $app["user_verify_mode"], "home_domain" => $app["home_domain"]];
				} else {
					$appConfig[$appConfigField] = $app[$appConfigField];
				}
			}
		}
		$appTable = [];
		if (is_array($options["appTable"])) {
			foreach ($options["appTable"] as $appTableName) {
				$data = \think\Db::name($appTableName)->where(["appid" => $app["id"]])->select();
				foreach ($data as $index => $item) {
					unset($data[$index]["id"]);
					unset($data[$index]["appid"]);
				}
				$appTable[$appTableName] = $data;
			}
		}
		$systemConfig = (new \app\common\system\System())->getConfig();
		$isHaveImportPassword = false;
		if (!empty($options["importPassword"])) {
			$isHaveImportPassword = true;
			$key = sha1(substr(md5($options["importPassword"]), 0, 16));
			$appConfig = base64_encode(openssl_encrypt(json_encode($appConfig), "AES-256-ECB", $key, OPENSSL_RAW_DATA));
			$appTable = base64_encode(openssl_encrypt(json_encode($appTable), "AES-256-ECB", $key, OPENSSL_RAW_DATA));
		}
		$result = ["appConfig" => $appConfig, "appTable" => $appTable, "isHaveImportPassword" => $isHaveImportPassword, "importPasswordVerificationKey" => !empty($options["importPassword"]) ? sha1(md5($options["importPassword"])) : "", "systemVersionNumber" => $systemConfig["version_number"], "systemVersionId" => $systemConfig["version_id"], "systemProductId" => $systemConfig["product_id"]];
		$result = base64_encode(openssl_encrypt(json_encode($result), "AES-256-ECB", md5("aimakeji-shuanq-app-key"), OPENSSL_RAW_DATA));
		return ["code" => \app\system\model\Code::success, "msg" => "导出成功", "data" => $result];
	}
	public static function import_app_config_data($options)
	{
		$app = \think\Db::name("app")->where(["id" => $options["appid"]])->find();
		if (empty($app)) {
			return ["code" => \app\system\model\Code::fail, "msg" => "id不存在"];
		}
		try {
			\think\Db::startTrans();
			$configData = openssl_decrypt(base64_decode($options["configData"]), "AES-256-ECB", md5("aimakeji-shuanq-app-key"), OPENSSL_RAW_DATA);
			if ($configData === false) {
				return ["code" => \app\system\model\Code::fail, "msg" => "导入失败，输入的配置数据不正确"];
			}
			$configData = json_decode($configData, true);
			$systemConfig = (new \app\common\system\System())->getConfig();
			if ($configData["systemProductId"] != $systemConfig["product_id"]) {
				return ["code" => \app\system\model\Code::fail, "msg" => "导入失败，配置数据的产品不对应"];
			}
			if ($configData["systemVersionId"] > $systemConfig["version_id"]) {
				return ["code" => \app\system\model\Code::fail, "msg" => "导入失败，此配置数据需要系统版本不低于 {$configData["systemVersionNumber"]} 版本标识 {$configData["systemVersionId"]}"];
			}
			$appConfig = $configData["appConfig"];
			$appTable = $configData["appTable"];
			if ($configData["isHaveImportPassword"] === true) {
				if (empty($options["importPassword"])) {
					return ["code" => \app\system\model\Code::fail, "msg" => "此配置数据存在密码验证，请输入导入密码"];
				}
				if (sha1(md5($options["importPassword"])) !== $configData["importPasswordVerificationKey"]) {
					return ["code" => \app\system\model\Code::fail, "msg" => "此配置数据存在密码验证，输入的导入密码不正确"];
				}
				$key = sha1(substr(md5($options["importPassword"]), 0, 16));
				$appConfig = json_decode(openssl_decrypt(base64_decode($appConfig), "AES-256-ECB", $key, OPENSSL_RAW_DATA), true);
				$appTable = json_decode(openssl_decrypt(base64_decode($appTable), "AES-256-ECB", $key, OPENSSL_RAW_DATA), true);
			}
			if (is_array($appConfig) && $appConfig) {
				foreach ($appConfig as $appConfigField => $data) {
					if ($appConfigField === "base") {
						\think\Db::name("app")->where(["id" => $app["id"]])->update($data);
					} else {
						\think\Db::name("app")->where(["id" => $app["id"]])->update([$appConfigField => $data]);
					}
				}
			}
			if (is_array($appTable) && $appTable) {
				foreach ($appTable as $appTableName => $dataList) {
					if ($dataList) {
						\think\Db::name($appTableName)->where(["appid" => $app["id"]])->delete();
						foreach ($dataList as $data) {
							$data["appid"] = $app["id"];
							\think\Db::name($appTableName)->insert($data);
						}
					}
				}
			}
			\think\Db::commit();
			return ["code" => \app\system\model\Code::success, "msg" => "导入出成功"];
		} catch (\Exception $e) {
			\think\Db::rollback();
			return ["code" => \app\system\model\Code::error, "msg" => $e->getTrace()];
		}
	}
	public static function get_feedback_config($options)
	{
		$item = \think\Db::name("app")->where(["id" => $options["id"]])->find();
		if (empty($item)) {
			return ["code" => \app\system\model\Code::fail, "msg" => "id不存在"];
		}
		$config = json_decode($item["feedback_config"], true);
		$data = ["helpList" => $config["helpList"] ?? [], "api_feedback_config" => ["enable_feedback" => $config["api_feedback_config"]["enable_feedback"] ?? false, "must_bring_pictures" => $config["api_feedback_config"]["must_bring_pictures"] ?? false, "must_bring_contact_info" => $config["api_feedback_config"]["must_bring_contact_info"] ?? false, "limit_wordage_minimum" => $config["api_feedback_config"]["limit_wordage_minimum"] ?? "", "limit_wordage_maximum" => $config["api_feedback_config"]["limit_wordage_maximum"] ?? "", "limit_once_random_interval_minimum" => $config["api_feedback_config"]["limit_once_random_interval_minimum"] ?? 0, "limit_once_random_interval_maximum" => $config["api_feedback_config"]["limit_once_random_interval_maximum"] ?? 0], "website_feedback_config" => ["enable_feedback" => $config["website_feedback_config"]["enable_feedback"] ?? false, "must_bring_pictures" => $config["website_feedback_config"]["must_bring_pictures"] ?? false, "must_bring_contact_info" => $config["website_feedback_config"]["must_bring_contact_info"] ?? false, "close_upload_file" => $config["website_feedback_config"]["close_upload_file"] ?? false, "limit_wordage_minimum" => $config["website_feedback_config"]["limit_wordage_minimum"] ?? "", "limit_wordage_maximum" => $config["website_feedback_config"]["limit_wordage_maximum"] ?? "", "template" => $config["website_feedback_config"]["template"] ?? "default", "limit_once_random_interval_minimum" => $config["website_feedback_config"]["limit_once_random_interval_minimum"] ?? 0, "limit_once_random_interval_maximum" => $config["website_feedback_config"]["limit_once_random_interval_maximum"] ?? 0]];
		return ["code" => \app\system\model\Code::success, "data" => $data];
	}
	public static function get_feedback_template()
	{
		$template_list = [];
		$dirs = getDirList("./view/feedback/");
		foreach ($dirs as $dir) {
			$copyright = "";
			$copyright_path = "./view/feedback/{$dir}/copyright.json";
			$copyright_path2 = "./view/feedback/{$dir}/static/copyright.json";
			$copyright_path3 = "./view/feedback/{$dir}/assets/copyright.json";
			$copyright_path4 = "./view/feedback/{$dir}/res/copyright.json";
			if (is_file($copyright_path)) {
				$copyright = file_get_contents($copyright_path);
			} elseif (is_file($copyright_path2)) {
				$copyright = file_get_contents($copyright_path2);
			} elseif (is_file($copyright_path3)) {
				$copyright = file_get_contents($copyright_path3);
			} elseif (is_file($copyright_path4)) {
				$copyright = file_get_contents($copyright_path4);
			}
			$template_config = json_decode($copyright, true) ?? [];
			$template_list[] = ["name" => $template_config["name"] ?? $dir, "template" => $dir, "author" => $template_config["author"] ?? "", "home" => $template_config["home"] ?? ""];
		}
		return ["code" => \app\system\model\Code::success, "data" => $template_list];
	}
	public static function set_feedback_config($options)
	{
		\think\Db::startTrans();
		try {
			$app = \think\Db::name("app")->where(["id" => $options["appid"]])->find();
			if (empty($app)) {
				return ["code" => \app\system\model\Code::fail, "msg" => "appid不存在"];
			}
			\think\Db::name("app")->where(["id" => $options["appid"]])->update(["feedback_config" => json_encode($options["config"])]);
			\think\Db::commit();
			return ["code" => \app\system\model\Code::success];
		} catch (\Exception $e) {
			\think\Db::rollback();
			return ["code" => \app\system\model\Code::error, "msg" => $e->getMessage()];
		}
	}
	public static function get_mail_config($options)
	{
		$item = \think\Db::name("app")->where(["id" => $options["id"]])->find();
		if (empty($item)) {
			return ["code" => \app\system\model\Code::fail, "msg" => "id不存在"];
		}
		$config = json_decode($item["mail_config"], true);
		$data = ["Host" => $config["Host"] ?? "", "Port" => $config["Port"] ?? "", "From" => $config["From"] ?? "", "Username" => $config["Username"] ?? "", "Password" => $config["Password"] ?? "", "FromName" => $config["FromName"] ?? "", "ReplyAddress" => $config["ReplyAddress"] ?? "", "ReplyAddressName" => $config["ReplyAddressName"] ?? "", "AltBody" => $config["AltBody"] ?? "", "CharSet" => $config["CharSet"] ?? "UTF-8", "SMTPSecure" => $config["SMTPSecure"] ?? "ssl"];
		return ["code" => \app\system\model\Code::success, "data" => $data];
	}
	public static function set_mail_config($options)
	{
		\think\Db::startTrans();
		try {
			$app = \think\Db::name("app")->where(["id" => $options["appid"]])->find();
			if (empty($app)) {
				return ["code" => \app\system\model\Code::fail, "msg" => "appid不存在"];
			}
			\think\Db::name("app")->where(["id" => $options["appid"]])->update(["mail_config" => json_encode($options["config"])]);
			\think\Db::commit();
			return ["code" => \app\system\model\Code::success];
		} catch (\Exception $e) {
			\think\Db::rollback();
			return ["code" => \app\system\model\Code::error, "msg" => $e->getMessage()];
		}
	}
	public static function mail_send_test($options)
	{
		$item = \think\Db::name("app")->where(["id" => $options["id"]])->find();
		if (empty($item)) {
			return ["code" => \app\system\model\Code::fail, "msg" => "id不存在"];
		}
		$config = json_decode($item["mail_config"], true);
		$body = "
        <div>
            <p>这是一封应用邮件配置内测试发出的邮件</p>
            <p><font color='red'>栓Q验证系统：<a href='http://shuanq.cn' target='_blank'>http://shuanq.cn</a> </font></p>
        </div>";
		$result = \app\common\mail\PHPMailer::send($config, "测试邮件", $body, $options["email"]);
		if ($result === true) {
			return ["code" => \app\system\model\Code::success];
		}
		return ["code" => \app\system\model\Code::fail, "msg" => $result];
	}
}